<?php $title='Fitness articles - edit'; ?>
<?php include('fns_db_articles.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>
<?php
if(!checkIfAdmin()){
	header("location: articles.php");
}
?>
<script language="JavaScript" type="text/javascript">
function formatText (tag) {
	tagstart = "<" + tag + ">";
	tagend = "</" + tag + ">";

	//code for Mozilla
	el = document.getElementById('articleText');
	if (el.setSelectionRange) {
 		el.value = el.value.substring(0,el.selectionStart) + tagstart + el.value.substring(el.selectionStart,el.selectionEnd) + tagend + el.value.substring(el.selectionEnd,el.value.length)
  	}
  	else {
  		//code for IE
	    var selectedText = document.selection.createRange().text;

	    if (selectedText != "") {
	        var newText = tagstart + selectedText + tagend;
	        document.selection.createRange().text = newText;
	    }
  	}
}

function setFontSize (size) {
	tagstart = "<span style='font-size:" + size + "'>";
	tagend = "</span>";

	//code for Mozilla
	el = document.getElementById('articleText');
	if (el.setSelectionRange) {
 		el.value = el.value.substring(0,el.selectionStart) + tagstart + el.value.substring(el.selectionStart,el.selectionEnd) + tagend + el.value.substring(el.selectionEnd,el.value.length)
  	}
  	else {
  		//code for IE
	    var selectedText = document.selection.createRange().text;

	    if (selectedText != "") {
	        var newText = tagstart + selectedText + tagend;
	        document.selection.createRange().text = newText;
	    }
  	}
}
</script>

<?php
if (isset($_GET['id']))
{
	$id = $_GET['id'];
	if ($id == '')
	{
		unset($id);
	}
}

/*
if (!isset($id))
{
	$result = mysqli_query($cxn, "SELECT title,id FROM articles");
	$myrow = mysqli_fetch_assoc($result);

	do
	{
		printf ("<p><a href='manage_articles.php?action=edit&id=%s'>%s</a></p>",$myrow["id"],$myrow["title"]);
	}while ($myrow = mysqli_fetch_assoc($result));

}
else
{*/
	$myrow = get_article_by_id($id);

	print <<<HERE

	<form name="form1" method="post" action="manage_articles.php?action=edit" enctype="multipart/form-data">

		  <table width="250" border="0" class="main-content-text" >
			  <tr>
				<td>Title:</td>
				<td><input value="$myrow[title]" type="text" name="title" id="title"></td>
			  </tr>
			  <tr>
				<td>Description:</td>
				<td><input value="$myrow[description]" type="text" name="description" id="description"></td>
			  </tr>
			  <tr>
				<td>Author:</td>
				<td><input value="$myrow[author]" type="text" name="author" id="author"></td>
			  </tr>
			  <tr>
	            <td>
	            	<img src="../images/articles/thumbnails/$myrow[img_path]" style="float:left;" hspace="10" vspace="10" border="0" align="texttop" onclick="window.open('$_SERVER[DOCUMENT_ROOT]/fitness/images/articles/$myrow[img_path]');"/>
	            </td>
	            <td>Change photo: <br/><input name="img_file" type="file"  />
	            <br/>(images must be less than 2MB and in jpg format)</td>
	          </tr>
		  </table>
		   <p>
				<input type="button" value="bold" onclick="formatText ('b');" />
				<input type="button" value="italic" onclick="formatText ('i');" />
				<input type="button" value="underline" onclick="formatText ('u');" /><br/>
				Set text size:
				<input type="button" value="large" onclick="setFontSize ('x-large');" />
				<input type="button" value="largest" onclick="setFontSize ('xx-large');" /><br/>
				<label>Text:<br/>
			   <textarea name="articleText" id="articleText" cols="130" rows="100" >$myrow[text]</textarea>
			   </label>
		  </p>

		 <input name="id" type="hidden" value="$myrow[id]">

		 <p>
		   <label>
		   <input type="submit" name="submit" id="submit" value="Save Changes">
		   </label>
		 </p>
	</form>

HERE;
//}
?>

<?php include('../views/layouts/_footer.php'); ?>